package com.example.tangjiangxu.permissiontest;

import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;
import com.zhy.m.permission.ShowRequestPermissionRationale;


/**
 * Android6.0 权限测试
 * Dangerours Permission:
 group:android.permission-group.CONTACTS
 permission:android.permission.WRITE_CONTACTS
 permission:android.permission.GET_ACCOUNTS
 permission:android.permission.READ_CONTACTS

 group:android.permission-group.PHONE
 permission:android.permission.READ_CALL_LOG
 permission:android.permission.READ_PHONE_STATE
 permission:android.permission.CALL_PHONE
 permission:android.permission.WRITE_CALL_LOG
 permission:android.permission.USE_SIP
 permission:android.permission.PROCESS_OUTGOING_CALLS
 permission:com.android.voicemail.permission.ADD_VOICEMAIL

 group:android.permission-group.CALENDAR
 permission:android.permission.READ_CALENDAR
 permission:android.permission.WRITE_CALENDAR

 group:android.permission-group.CAMERA  //相机权限
 permission:android.permission.CAMERA

 group:android.permission-group.SENSORS
 permission:android.permission.BODY_SENSORS

 group:android.permission-group.LOCATION    ／／位置信息相关
 permission:android.permission.ACCESS_FINE_LOCATION
 permission:android.permission.ACCESS_COARSE_LOCATION

 group:android.permission-group.STORAGE
 permission:android.permission.READ_EXTERNAL_STORAGE
 permission:android.permission.WRITE_EXTERNAL_STORAGE

 group:android.permission-group.MICROPHONE
 permission:android.permission.RECORD_AUDIO

 group:android.permission-group.SMS
 permission:android.permission.READ_SMS
 permission:android.permission.RECEIVE_WAP_PUSH
 permission:android.permission.RECEIVE_MMS
 permission:android.permission.RECEIVE_SMS
 permission:android.permission.SEND_SMS
 permission:android.permission.READ_CELL_BROADCASTS
 */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    private static final int REQUEST_CODE_READ_CONTACTS = 1;
    private static final int REQUEST_CODE_SDCARD = 2;

    private Button bnCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     *
     * @param view
     */
    public void onCall(View view) {
        call(this, this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_CODE_SDCARD);
        //callPhone();
    }

    /**
     * 权限申请的回调
     * @param requestCode 申请的code
     * @param permissions  权限组
     * @param grantResults 赋予权限结果数组
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
       //MPermissions.onRequestPermissionsResult(this, requestCode, permissions,grantResults);
        switch (requestCode) {
            case REQUEST_CODE_READ_CONTACTS:
                if (grantResults != null) {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        //授权成功
                        Log.e(TAG, "call: 申请权限成功");
                        callPhone();
                    } else if (grantResults[0] == PackageManager.PERMISSION_DENIED
                            && !ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
                        //如果用户点了拒绝，且勾选了不再询问
                        Log.e(TAG, "call: 拒绝了权限，且勾选了不再询问");
                    } else {
                        //点了拒绝，但是没有勾选不再询问
                        Log.e(TAG, "call: 拒绝了权限，且没有勾选不再询问");
                    }
                } else {
                    Log.e(TAG, "call: 拒绝了权限");
                }
                break;
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    private void call(Context context, Activity activity, String permissionName, int requestCode) {
        //Log.e(TAG, "call: 是否显示框：" + MPermissions.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, requestCode));
       /* if (!MPermissions.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, requestCode))
        {
            MPermissions.requestPermissions(activity, requestCode, permissionName);
        }*/
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            //没有权限，申请权限
            Log.e(TAG, "call: 没有权限，申请权限");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_READ_CONTACTS);
        } else {
            //有权限，拨打电话
            Log.e(TAG, "call: 有权限，拨打电话");
            callPhone();
        }


    }

    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);

        startActivity(intent);
    }

    /*@ShowRequestPermissionRationale(REQUEST_CODE_SDCARD)
    public void whyNeedSdCard() {
        Toast.makeText(this, "为什么需要sd权限",Toast.LENGTH_SHORT).show();
        MPermissions.requestPermissions(this, REQUEST_CODE_SDCARD, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @PermissionGrant(REQUEST_CODE_SDCARD)
    public void requestSdcardSuccess() {
        Toast.makeText(this, "申请sd卡权限成功", Toast.LENGTH_SHORT).show();
    }

     @PermissionDenied(REQUEST_CODE_SDCARD)
    public void requestSdcardFailed() {
         Toast.makeText(this, "申请sd卡权限失败", Toast.LENGTH_SHORT).show();
    }*/


}
